// Copyright  2004-2008 Backplane Ltd.
//
// This file is part of RegistryLookup
//
// RegistryLookup is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3 of the License, or (at your
// option) any later version.
//
// RegistryLookup is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License
// along with RegistryLookup. If not, see <http://www.gnu.org/licenses/>.

//	
//
//	Purpose: Definition of CGenericRequestBroker
//   $Id$

#pragma once

#include "resource.h"
#include <RegistryLookup.h>
#include <Common/COM/msw/multiIDispatch.h>
#include <atlctl.h>

typedef IDispatchImpl<IRegistryRead, &IID_IRegistryRead, &LIBID_RegistryLookupLib, /*wMajor =*/ 1, /*wMinor =*/ 0> GRBDispatchBase1;
typedef IDispatchImpl<IRegistryWrite, &IID_IRegistryRead, &LIBID_RegistryLookupLib, /*wMajor =*/ 1, /*wMinor =*/ 0> GRBDispatchBase2;

class ATL_NO_VTABLE CGenericRequestBroker : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CGenericRequestBroker, &CLSID_GenericRequestBroker>,
	public GRBDispatchBase1,
	public GRBDispatchBase2,
	public ISupportErrorInfo,
	public IObjectSafetyImpl<CGenericRequestBroker, INTERFACESAFE_FOR_UNTRUSTED_CALLER|INTERFACESAFE_FOR_UNTRUSTED_DATA>,
	public CMultiIDispatch<CGenericRequestBroker>
{
public:
	// Construction and destruction.
	CGenericRequestBroker();
	virtual ~CGenericRequestBroker();

	DECLARE_REGISTRY_RESOURCEID(IDR_GENERICREQUESTBROKER)

	BEGIN_COM_MAP(CGenericRequestBroker)
		COM_INTERFACE_ENTRY2(IDispatch, IRegistryRead)
		COM_INTERFACE_ENTRY(IRegistryRead)
		COM_INTERFACE_ENTRY(IRegistryWrite)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
		COM_INTERFACE_ENTRY(IObjectSafety)
	END_COM_MAP()

#pragma warning(disable:4244)
	BEGIN_MULTI_DISPATCH_MAP(CGenericRequestBroker)
		MULTI_DISPATCH_ENTRY(GRBDispatchBase1)
		MULTI_DISPATCH_ENTRY(GRBDispatchBase2)
	END_MULTI_DISPATCH_MAP()
#pragma warning(default:4244)

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

	// IRegistryRead.
	STDMETHOD(ReadValueString)(BSTR sKeyRoot, BSTR sKey, BSTR sValue, BSTR *psData);

	// ISupportErrorInfo.
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

	DECLARE_MULTI_DISPATCH(CGenericRequestBroker)

protected:
	// Request authorisation to read the specified registry value.
	HRESULT AuthoriseValueRead(BSTR sKeyRoot, BSTR sKey, BSTR sValue);

	// Convert a string into its corresponding HKEY value.
	HRESULT GetHKeyFromString(const BSTR sKey, HKEY &hKey);

	// Convert data from the registry into an appropriate string representation.
	HRESULT GetStringFromData(const LPBYTE pData, const DWORD dwDataLen, const DWORD dwDataType, BSTR *psData);

private:
	// Prohibited operations.
	CGenericRequestBroker(const CGenericRequestBroker &rhs);
	CGenericRequestBroker &operator=(const CGenericRequestBroker &rhs);
};

OBJECT_ENTRY_AUTO(__uuidof(GenericRequestBroker), CGenericRequestBroker)
